﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace HuaCheng.Controller
{
    class UserPictureControl
    {

        /// <summary>
        /// 创建一个空的数据表,只定义了列，没有指定行
        /// </summary>
        /// <returns></returns>
        public static DataTable CreateTable()
        {
            //创建一个带名字的表格
            DataTable dt = new DataTable("graph");

            //创建列
            DataColumn dc = new DataColumn("Time", typeof(double));
            dt.Columns.Add(dc);
            dc = new DataColumn("Voltage", typeof(float));
            dt.Columns.Add(dc);
            dc = new DataColumn("Current", typeof(float));
            dt.Columns.Add(dc);

            return dt;

        }

        /// <summary>
        /// 更新表
        /// </summary>
        /// <returns></returns>
        public static DataTable UpdateTable(DataTable dt, DateTime nowTime, float voltage, float current)
        {

            lock (dt)
            {
                if (dt.Rows.Count < 500)
                {
                    dt.Rows.Add(nowTime.ToOADate(), voltage, current);
                }
                else
                {
                    //去掉最早的那一行，然后再加入新row
                    dt.Rows.Remove(dt.Rows[0]);
                    dt.Rows.Add(nowTime.ToOADate(), voltage, current);
                }
            }
            return dt;
        }

        /// <summary>
        /// 复制表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable CopyTable(DataTable dt)
        {
            //lock (dt)
            //{
                DataTable dtNew = dt.Copy();  //复制dt表数据结构
            //    dtNew.Clear();  //清除数据
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        if (dt.Rows[i] != null)
            //            dtNew.Rows.Add(dt.Rows[i].ItemArray);  //添加数据行
            //    }

                return dtNew;
            //}

        }

        /// <summary>
        /// 复制数据,深度复制
        /// </summary>
        /// <param name="dataSource">数据源，待复制的数据</param>
        /// <returns></returns>
        public static DataTable DeepCopy(DataTable dataSource)
        {
            DataTable dataTable = new DataTable();
            //定义表结构
            DataColumn col;
            foreach (DataColumn column in dataSource.Columns)
            {
                col = new DataColumn();
                col.ColumnName = column.ColumnName;
                col.DataType = column.DataType;
                dataTable.Columns.Add(col);
            }
            foreach (DataRow row in dataSource.Rows)
            {
                DataRow tempRow = dataTable.NewRow();
                foreach (DataColumn column in dataSource.Columns)
                {
                    try
                    {
                        tempRow[column.ColumnName] = row[column.ColumnName];
                    }
                    catch
                    { continue; }
                }
                dataTable.Rows.Add(tempRow);
            }
            return dataTable;
        }
    }
}
